package com.github.donghaichen.demo;


import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileFilter;

//@EnableScheduling
@Log4j2
@RestController
public class Dwg2PDF {

    @GetMapping("dwg2PDF")
    public static void dwg2PDF(String[] args) throws Exception {
        String sourcePath = "D:\\cad\\dwg";
        String distPath = "D:\\cad\\pdf";
        String softPath = "C:\\Users\\info\\Desktop\\Acme CAD Converter\\AcmeCADConverter";
        String cmdTemplate = softPath + " /r /e /ls /f 105 /a -2 /bookmark /hide 1 " + ///w 1920 /h 1080
                "/d \"%s\" \"%s\"";

        File sourceDir = new File(sourcePath);
        File[] files = sourceDir.listFiles(new FileFilter() {
            String fileName;

            @Override
            public boolean accept(File pathname) {
                fileName = pathname.getName();
                if (fileName.endsWith("dwg") || fileName.endsWith("DWG") || fileName.endsWith("dxf") || fileName.endsWith("DXF")) {
                    return true;
                }
                return false;
            }
        });
        if (files == null) {
            System.out.println("No Dwg or Dxf.");
            return;
        }
        String fileNamePost;

        for (File file : files) {
            fileNamePost = file.getName();
            Runtime run = Runtime.getRuntime();
            try {
                String cmd = String.format(cmdTemplate, distPath + "\\" + fileNamePost + ".pdf", sourcePath + "\\" + fileNamePost);
                Process process = run.exec(cmd);
                while (process.isAlive()) {

                }
                System.out.println(fileNamePost + " -> Done!");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}
